// call init
$(init);
var width_menuCat = 0;

function init() {
  // 1. onload ajax hash 
  var $href = $(window.location).attr("hash");
  if ($href) {
	  page_load($(window.location).attr("hash")); // goto first page if #!: is available
  } else {
	  loadHomePage(); 
  } 	
  // 2. get event hash
  ajax_page_handler(); 
  // 3. select list
  selectCate(); 
  // ------------------
  $('.tabs-prod li#item-spec a').live('click', function () {
		$(this).scrollTopObj({
			topHeight: 40
			,objMove  :'.specs-list'
		});				
		$(this).addClass('arrow-down');
	});
}
 
function page_load($href) {
	var link_a;	
  if($href != undefined && $href.substring(0, 2) == '#/') {
	link_a = $href.substring(2);
	exloadPage(link_a);
  } else if ($href.substring(0, 3) == '#?/')  {
	link_a = $href.substring(3);   
	cateloadPage(link_a, 'html');
  }
}

function exloadPage(linka) {
	if ($('.home').css('display') == 'block') {
		$('.home').removeClass('animated  fadeInRightBig')
				  .addClass('animated  fadeOutLeftBig')
				  .fadeOut(1000);
	} else {
		$('.sub_page').removeClass('animated  fadeInRightBig').addClass('animated  fadeOutLeftBig');
	}
	// ajax HERE  
	$.ajax({
		  type: "GET",
		  url: linka,
		  dataType: "html"
		}).done(function( data ) {			
			$('.sub_page').removeClass('animated  fadeOutLeftBig')
						  .addClass('animated  fadeInRightBig')
						  .show().html(data);
			poupProd();
			selectCate();
			getVideo();
			loadMore($('.loadmore'),$('.loading_ct ul'));
			loadMore($('.loadmore_cm'),$('#all-opinions'));
			
	});
   // $('#content').load($href.substring(2)); // replace body the #content with loaded html
    $('html, body').animate({scrollTop:0}, 'slow'); // bonuslink
}

function cateloadPage(linka, ltype) {
	
	$('.loading_ct ul').removeClass('animated  fadeInRightBig').addClass('animated  fadeOutLeftBig');	
	// ajax HERE  
	$.ajax({
		  type: "GET",
		  url: linka,
		  dataType: "html"
		}).done(function( data ) {	
			if (ltype == 'append') {
				$('.loading_ct ul').removeClass('animated  fadeOutLeftBig')
				  .addClass('animated  fadeInRightBig')
				  .show().append(data);			
			} else {
				$('.loading_ct ul').removeClass('animated  fadeOutLeftBig')
				  .addClass('animated  fadeInRightBig')
				  .show().html(data);			
			}
		    
	});
    $('html, body').animate({scrollTop:0}, 'slow'); // bonuslink
}

/**
 * This method load loadHomePage on every url hash change to home
 *
 * @return
 */
function loadHomePage () {	
		if ($('.home').css('display') != 'block' ) {
			$('.home').removeClass('animated  fadeOutLeftBig').addClass('animated  fadeInRightBig').show();
			$('.sub_page').removeClass('animated  fadeInRightBig').addClass('animated  fadeOutLeftBig').fadeOut(1000);
		}
} 

/**
 * This method load main content on every url hash change
 *
 * @return
 */
function ajax_page_handler() {
  $(window).bind('hashchange', function () {
	    $href = $(window.location).attr("hash");
	    if ($href) {	       
	    	page_load($href); // goto first page if #!: is available
	    } else {
	  	   loadHomePage(); 
	    } 	
  });  	
//	 $('a[href^="#/"]').live('click',  function () {
//		 $href = $(this).attr("href");
//		 //alert($(this).attr("href"));
//		    if ($href) {
//		    	//alert($href);
//		  	   page_load($href); // goto first page if #!: is available
//		    } else {
//		  	   loadHomePage(); 
//		    } 	     
//	  }); 
}

/**
 * This method load category content by select list
 *
 * @return
 */
function selectCate() {
	var next_link; 	
	if ($('.home .menu_cate .menu_cate_ct').find('a').length) {
		if (width_menuCat < 900) {
			$('.home .menu_cate .menu_cate_ct').find('a').each(function(index) {
				width_menuCat += $(this).outerWidth(true);
			});	
			if ($.browser.mozilla) {
				width_menuCat += 10;	
			}
		}		
		$('.menu_cate .menu_cate_ct').css({'width': width_menuCat+'px'});
		$('.menu_cate .menu_cate_ct').on('click', 'a', function () {
			// get id to load category
			$('.menu_cate .menu_cate_ct a').removeClass('menu_active');
			$(this).addClass('menu_active');	
			$('h3.title span').text($(this).text());
		});
		
		// MENU can scroll and has effect
		var posScroll, posEnd;
		$('.menu_cate').on('scroll', function () {
			posScroll = $('.menu_cate').scrollLeft();
			posEnd    = $('.menu_cate .menu_cate_ct').width()-$(window).width();
			if (posScroll <= 0 ) {
				$('.l_shadow').fadeOut('500');
				$('.r_shadow').fadeIn('500');
			}
			if (posScroll > 0 && posScroll < posEnd) {
				$('.l_shadow').fadeIn('500');
				$('.r_shadow').fadeIn('500');
			}
			if (posScroll == posEnd) {
				$('.r_shadow').fadeOut('500');	
				$('.l_shadow').fadeIn('500');
			}			
		});
	} // END MENU cate news
	
	// FOR selec list in Product cate. 
	$('.slt_cate').live('change', function () {
		// get id to load category
		next_link = $(this).val();
		// ---------------------
		$(window.location).attr("hash",next_link);
	});
}

/**
 * This method load more content by select list
 *
 * @return
 */
function loadMore(o_click, o_load) {
	var next_link; 
	o_click.live('click', function () {
		// get id to load category
		next_link = jQuery.trim($(this).attr('title'));
		// next_link = 'page/news_cat/cate.html';
		// ---------------------
		// ajax HERE  
		o_click.find('span').text('TẢI ...');
		$.ajax({
			  type: "GET",
			  url: next_link,
			  dataType: "html"
			}).done(function( data ) {					
					o_load.append(data);	
					o_click.find('span').text('XEM THÊM');
		});
	});
}
// LAZY LOAD IMG LIB
(function(a,b){var c=a(b);a.fn.lazyload=function(d){function h(){var b=0;e.each(function(){var c=a(this);if(g.skip_invisible&&!c.is(":visible"))return;if(!a.abovethetop(this,g)&&!a.leftofbegin(this,g))if(!a.belowthefold(this,g)&&!a.rightoffold(this,g))c.trigger("appear"),b=0;else if(++b>g.failure_limit)return!1})}var e=this,f,g={threshold:0,failure_limit:0,event:"scroll",effect:"show",container:b,data_attribute:"original",skip_invisible:!0,appear:null,load:null};return d&&(undefined!==d.failurelimit&&(d.failure_limit=d.failurelimit,delete d.failurelimit),undefined!==d.effectspeed&&(d.effect_speed=d.effectspeed,delete d.effectspeed),a.extend(g,d)),f=g.container===undefined||g.container===b?c:a(g.container),0===g.event.indexOf("scroll")&&f.bind(g.event,function(a){return h()}),this.each(function(){var b=this,c=a(b);b.loaded=!1,c.one("appear",function(){if(!this.loaded){if(g.appear){var d=e.length;g.appear.call(b,d,g)}a("<img />").bind("load",function(){c.hide().attr("src",c.data(g.data_attribute))[g.effect](g.effect_speed),b.loaded=!0;var d=a.grep(e,function(a){return!a.loaded});e=a(d);if(g.load){var f=e.length;g.load.call(b,f,g)}}).attr("src",c.data(g.data_attribute))}}),0!==g.event.indexOf("scroll")&&c.bind(g.event,function(a){b.loaded||c.trigger("appear")})}),c.bind("resize",function(a){h()}),a(document).ready(function(){h()}),this},a.belowthefold=function(d,e){var f;return e.container===undefined||e.container===b?f=c.height()+c.scrollTop():f=a(e.container).offset().top+a(e.container).height(),f<=a(d).offset().top-e.threshold},a.rightoffold=function(d,e){var f;return e.container===undefined||e.container===b?f=c.width()+c.scrollLeft():f=a(e.container).offset().left+a(e.container).width(),f<=a(d).offset().left-e.threshold},a.abovethetop=function(d,e){var f;return e.container===undefined||e.container===b?f=c.scrollTop():f=a(e.container).offset().top,f>=a(d).offset().top+e.threshold+a(d).height()},a.leftofbegin=function(d,e){var f;return e.container===undefined||e.container===b?f=c.scrollLeft():f=a(e.container).offset().left,f>=a(d).offset().left+e.threshold+a(d).width()},a.inviewport=function(b,c){return!a.rightofscreen(b,c)&&!a.leftofscreen(b,c)&&!a.belowthefold(b,c)&&!a.abovethetop(b,c)},a.extend(a.expr[":"],{"below-the-fold":function(b){return a.belowthefold(b,{threshold:0})},"above-the-top":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-screen":function(b){return a.rightoffold(b,{threshold:0})},"left-of-screen":function(b){return!a.rightoffold(b,{threshold:0})},"in-viewport":function(b){return!a.inviewport(b,{threshold:0})},"above-the-fold":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-fold":function(b){return a.rightoffold(b,{threshold:0})},"left-of-fold":function(b){return!a.rightoffold(b,{threshold:0})}})})(jQuery,window)